package org.flexlancer.CommentsLibrary
{
	import flash.display.Graphics;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	
	import mx.containers.Canvas;
	import mx.core.ScrollPolicy;
	
	import org.flexlancer.Logger.Logger;
	import org.flexlancer.utils.DateUtils;

	public class CommentManager extends Canvas
	{
		/*
		Variabels
		**/
		private var _comments_array:Array = new Array();
		private var _user_name:String = "";
		public var prt_width:Number;
		public var prt_height:Number;
		
		//public var insert:CommentObject;
		public var n:Number = 0;
		public var commentsXML:XML;
		private var __scale:Number = 1;
		
		/*
		Properties
		**/
		public function set UserName(val:String):void{
			this._user_name = val;
		}
		public function set ContainerWidth(val:Number):void{
			this.prt_width = val;
		}
		public function set ContainerHeight(val:Number):void{
			this.prt_height = val;
		}
		public function get ContainerWidth():Number{
			return this.prt_width;
		}
		public function get ContainerHeight():Number{
			return this.prt_height;
		}
		public function set Scale(val:Number):void{
			this.__scale = val;
			this.scaleX = this.scaleY = val;
			for each ( var singleCommentObject:CommentObject in this._comments_array ){
				singleCommentObject.Scale = val;
			}
		}
		/*
		Implementation
		**/
		
		/*
		Constructor
		**/
		public function CommentManager(parmUserName:String, 
														parmContainerWidth:Number,
														parmContainerHeight:Number)
		{
			try{
				super();
				this._user_name = parmUserName;
				this.prt_width = parmContainerWidth;
				this.prt_height = parmContainerHeight;
				//this.setStyle("borderStyle","solid");
				//this.setStyle("borderColor",0x00ff00);
				this.horizontalScrollPolicy = this.verticalScrollPolicy = ScrollPolicy.OFF;
			}catch(err:Error){
				throw err;
			}
		}
		
		public function AddCommentFromXML(singleCData:XML):void {
				try{
						var tempRectangle:Rectangle = new Rectangle(0,0,singleCData.@boundwidth,singleCData.@boundheight);
						this.AddComment(tempRectangle,singleCData.@commentname,singleCData.commentitem.message,singleCData.@x,singleCData.@y,singleCData);
				//Alert.show(this.commentsXML.toXMLString());
			} catch(err:Error){
				throw err;
			}
		}
		
		public function AddComment(parmBound:Rectangle, parmName:String = "", parmMessege:String = "", cX:Number= 0, cY:Number=0, items:XML = null):String{
			try{
				var pName:String;
				if (parmName==""){
					this.n++;
					pName = this.n.toString();	
				}else{
					pName = parmName;
					n = Number(pName);
				}
				var insert:CommentObject = new CommentObject(this._user_name, parmMessege, parmBound, pName);
				//insert._btn_indicator.x = cX;
				//insert._btn_indicator.y = cY;
				//insert.setStyle("borderStyle","solid");
				insert.x = cX;
				insert.y = cY;
				insert.Scale = this.__scale;
				insert.name = pName;
				insert._output_panel.title += pName;
				if(items!=null){
					for each ( var singleItem:XML in items.commentitem){
						var msg:String = singleItem.messege;
						var dateandtime:Date = DateUtils.ConvertintoDate(singleItem.date);
						//Alert.show(dateandtime.toString());
						insert.AddComment(msg,dateandtime);
					} 
				}
				this._comments_array.push(insert);
				this.addChild(insert);
				return insert.name;							
			}catch(err:Error){
				throw err;
			}
			return "";
		}
		
		public function RemoveCommentbyName (commentName:String):void{
				try{
					//Alert.show(commentName.toString());
					var chd:CommentObject = this.getChildByName(commentName) as CommentObject;
					
					if ( chd != null ){
						
						this.removeChild(chd);
						
						}
						
				}catch(err:Error){
					throw err;
				}
			} 
						
		public function isAnyCommentMove():Boolean{
				try{
					if ( this._comments_array != null ){
						for each ( var singleComment:CommentObject in this._comments_array){
							if ( singleComment.CommentMove ){
								return true;
							}
						}
					}
				}catch(err:Error){
					throw err;
				}
				return false; 
			} 
		
		public function DeleteComment(parmComment:CommentObject):CommentObject{
			try{
				var index:Number = this._comments_array.indexOf(parmComment);
				var retValue:CommentObject = null;
				if ( index > -1 ){
					retValue = this._comments_array[index];
					this.removeChild(this._comments_array[index]);
					this._comments_array.splice(index,1);
				}
				return retValue;
			}catch(err:Error){
				throw err;
			}
			return null;
		}
		
		public function getXMLData():XML{
			try{
				var xml_data:XML = new XML(<comments/>);
				for each ( var singleObject:CommentObject in this._comments_array ){
					var singleRes:XML = singleObject.getXMLDatat();
					if ( singleRes != null )
						xml_data.appendChild(singleRes);
				}
				return xml_data;
			}catch(err:Error){
				throw err;
			}
			return null;
		}
		
		override public function set rotation(value:Number):void{
			try{
				super.rotation = 0;
				for each ( var sobject:CommentObject in this._comments_array ){
					sobject.rotation = value;
				}
			}catch(err:Error){
				Logger.LogE(err);
			}
		}
	}
}